package com.nsgf.library.ui.mine;


import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.binding_recyclerview.adapter.SimpleDataBindingAdapter;
import com.kunminx.binding_state.BR;
import com.nsgf.library.data.bean.LibraryInfo;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.StateHolder;
import com.nsgf.library.ui.product.BusinessDetailActivity;
import com.nsgf.library.ui.product.ProductDetailActivity;
import com.zhht.htzx.R;
import com.zhht.htzx.databinding.ActivitySignInBinding;
import com.zhht.htzx.databinding.ItemSignInDayBinding;
import com.zhht.htzx.databinding.ItemSignRecordBinding;

import java.util.List;


public class SignInActivity extends BaseActivity {

    private StateHolder mStates;
    private AccountRequester mAccountRequester;

    private SimpleDataBindingAdapter<LibraryInfo, ItemSignInDayBinding> mAdapter;

    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(StateHolder.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        mAdapter = new SimpleDataBindingAdapter<LibraryInfo, ItemSignInDayBinding>(this, R.layout.item_sign_in_day, new DiffUtil.ItemCallback<LibraryInfo>() {
            @Override
            public boolean areItemsTheSame(@NonNull LibraryInfo oldItem, @NonNull LibraryInfo newItem) {
                return false;
            }

            @Override
            public boolean areContentsTheSame(@NonNull LibraryInfo oldItem, @NonNull LibraryInfo newItem) {
                return false;
            }
        }) {
            @Override
            protected void onBindItem(ItemSignInDayBinding binding, LibraryInfo item, RecyclerView.ViewHolder holder) {
                binding.setVm(item);

                if (item.isCheck()) {
                    binding.dayCard.setBackgroundResource(R.drawable.bg_sign_button_selected);
                    binding.tvItemDay.setTextColor(getResources().getColor(android.R.color.white));
                    binding.tvItemPoint.setTextColor(getResources().getColor(android.R.color.white));
                } else {
                    binding.dayCard.setBackgroundResource(R.drawable.bg_sign_button);
                    binding.tvItemDay.setTextColor(getResources().getColor(R.color.color_333333));
                    binding.tvItemPoint.setTextColor(getResources().getColor(R.color.color_333333));
                }
            }
        };

        SimpleDataBindingAdapter mAdapterRecord = new SimpleDataBindingAdapter<LibraryInfo, ItemSignRecordBinding>(this, R.layout.item_sign_record, new DiffUtil.ItemCallback<LibraryInfo>() {
            @Override
            public boolean areItemsTheSame(@NonNull LibraryInfo oldItem, @NonNull LibraryInfo newItem) {
                return false;
            }

            @Override
            public boolean areContentsTheSame(@NonNull LibraryInfo oldItem, @NonNull LibraryInfo newItem) {
                return false;
            }
        }) {
            @Override
            protected void onBindItem(ItemSignRecordBinding binding, LibraryInfo item, RecyclerView.ViewHolder holder) {
                binding.setVm(item);
            }
        };
        return new DataBindingConfig(R.layout.activity_sign_in, BR.vm, mStates)
                .addBindingParam(BR.adapterRecord, mAdapterRecord)
                .addBindingParam(BR.click, new ClickProxy())
                .addBindingParam(BR.adapter, mAdapter);
    }

    private ActivitySignInBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLifecycle().addObserver(mAccountRequester);
        mStates.name.set(UserInfo.getInstance().getName());
        mStates.titleBar.set("签到领积分");
        mBinding = (ActivitySignInBinding) getBinding();
        mBinding.titlebar.titleBarRoot.setTextColor(getResources().getColor(R.color.white));
        mBinding.titlebar.titleBarBack.setImageResource(R.drawable.ic_titlebar_white_back);
        mBinding.recyclerView.setNestedScrollingEnabled(false);
        mAccountRequester.getLibraryList().observe(this, new Observer<ResponseStatus<List<LibraryInfo>>>() {
            @Override
            public void onChanged(ResponseStatus<List<LibraryInfo>> listResponseStatus) {

                if (listResponseStatus.getResponseCode() == 0) {
                    mStates.testList.set(listResponseStatus.getData());
                }
                mAccountRequester.getSignSummary();

            }
        });
        mAccountRequester.getRainRecordList().observe(this, new Observer<ResponseStatus<List<LibraryInfo>>>() {
            @Override
            public void onChanged(ResponseStatus<List<LibraryInfo>> listResponseStatus) {

                if (listResponseStatus.getResponseCode() == 0) {
                    mStates.signRecordList.set(listResponseStatus.getData());

                }
            }
        });

        mAccountRequester.getLibraryInfo().observe(this, new Observer<ResponseStatus<LibraryInfo>>() {
            @Override
            public void onChanged(ResponseStatus<LibraryInfo> libraryInfoResponseStatus) {
                if (libraryInfoResponseStatus.getResponseCode() == 0) {
                    mStates.registerInfo.set(libraryInfoResponseStatus.getData());
                }
            }
        });
        mAccountRequester.getSignSummaryResult().observe(this, new Observer<ResponseStatus<LibraryInfo>>() {
            @Override
            public void onChanged(ResponseStatus<LibraryInfo> libraryInfoResponseStatus) {

                if (libraryInfoResponseStatus.getResponseCode() == 0) {
                    mStates.signSummaryResult.set(libraryInfoResponseStatus.getData());

                    for (LibraryInfo info : mAdapter.getCurrentList()) {
                        if (info.getDay().equals(libraryInfoResponseStatus.getData().getContinuousDay())) {
                            if (libraryInfoResponseStatus.getData().isTodaySignIn()) {
                                info.setCheck(true);
                                mBinding.tvSignBtn.setText("今日已签到");
                                mAdapter.notifyDataSetChanged();
                                break;
                            }
                        }
                    }
                }
            }
        });
        mAccountRequester.getSrcSuccess().observe(this, new Observer<ResponseStatus<String>>() {
            @Override
            public void onChanged(ResponseStatus<String> stringResponseStatus) {

                if (stringResponseStatus.getResponseCode()==0){
                    mAccountRequester.getSignSummary();
                }

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mAccountRequester.getSignInList();
        mAccountRequester.getSignInRecords();
        mAccountRequester.queryUserPoint();
    }

    public class ClickProxy {

        public void signIn() {
            if (!"今日已签到".equals(mBinding.tvSignBtn.getText().toString())) {
                startActivity(new Intent().setClass(SignInActivity.this,VideoActivity.class));
            }
        }

    }

}